<?php

defined('_JEXEC') or die('Restricted Access');

class DoskaControllerCategories extends JControllerAdmin {
	
	
	public function getModel($name = 'Category',$prefix = 'DoskaModel',$config = array()) {
		return parent::getModel($name,$prefix,$config);
	}
	
	public function delete()
	{
		
		
		JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));
		
		$app = JFactory::getApplication();
		
		$cid = $this->input->get('cid',array(),'array');
		
		if(empty($cid) || !is_array($cid)) {
			$app->enqueueMessage(JText::_('COM_DOSKA_MESSAGE_DELETE_CATEGORIES'),'notice');
		}
		
		foreach($cid as $id) {
			$flag = FALSE;
			$db = JFactory::getDbo();
			$query = $db->getQuery(true);
			
			$query->select('COUNT(*)');
			$query->from('#__doska_categories');
			$query->where('parentid='.$id);
			
			
			$db->setQuery($query);
			
			try {
				$row = $db->loadResult();
				
				if($row > 0) {
	$app->enqueueMessage(JText::_('COM_DOSKA_MESSAGE_DELETE_CATEGORY_IS_PARENT'),'notice');	
	break;
				}
				elseif($row === '0') {
					$flag = TRUE;
				}
			}
			catch(RuntimeException $e) {
				$app->enqueueMessage(JText::_('COM_DOSKA_MESSAGE_DELETE_CATEGORIES'),'error');
				break;
			}
			
			
		}
		
		if($flag === TRUE) {
			parent::delete();
		}
		
		$this->setRedirect(JRoute::_('index.php?option='.$this->option.'&view='.$this->view_list,false));
		
	}	
	
	
}